import { BasicColumn, FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import { DescItem } from '/@/components/Description/index';
import { UploadBizType } from '/@/enums/uploadEnum';
import { useUserStore } from '/@/store/modules/user';
import { TenantType } from '/@/enums/bizEnum'
const userStore = useUserStore()
const selectTenant = userStore.getSelectTenant
export const columns: BasicColumn[] = [
  {
    title: '租户名称',
    dataIndex: 'name',
    width: 130,
    // slots: { customRender: 'tenantName' },
    align: 'left',
    // slots: { customRender: 'requestMethod' }
  },
  {
    title: '租户编号',
    dataIndex: 'id',
    width: 160,
  },

  {
    title: '内置',
    width: 60,
    customRender: ({ text }) => {
      return render.renderTag(text ? '是' : '否', 'green')
    },
    dataIndex: 'sysBuiltIn',
  },
  {
    title: '租户类型',
    width: 80,
    customRender: (data) => {
      const { text, record } = data as any
      let tenantTypeName = record.tenantTypeName
      let color;
      if (record.id === '1') {
        color = 'purple'
        tenantTypeName = '平台端'
      }else if (text === '2') {
        color = 'blue'
      } else if (text === '3') {
        color = 'green'
      } else if (text === '4') {
        tenantTypeName = '子租户'
        color = 'cyan'
      }
      return render.renderTag(tenantTypeName, color)
    },
    dataIndex: 'tenantType',
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt',
    sorter: true,
    width: 160
  },
  {
    title: '创建者',
    dataIndex: 'createdBy',
    sorter: true,
    width: 160
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 60,
    customRender: ({ text }) => {
      const color = text === '1' ? 'green' : 'red';
      return render.renderTag(render.renderDict(text, 'data_use_status'), color)
    }
  },

];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '租户名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'data_use_status',
      placeholder:'请选择状态',
      stringToNumber:true
    },
    colProps: { span: 8 },
  },
  {
    field: 'fieldTime',
    component: 'RangePicker',
    label: '时间字段',
    componentProps: {
      valueType: 'Date',
    },
    colProps: {
      span: 8,
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    required: false
  },
  {
    field: 'adminUserId',
    label: 'adminUserId',
    component: 'Input',
    show: false,
    required: false
  },
  {
    field: 'name',
    label: '租户名称',
    component: 'Input',
    required: true
  },
  {
    field: 'tenantType',
    label: '租户类型',
    component: 'JDictSelectTag',
    required: true,
    ifShow: () => selectTenant.tenantType === TenantType.PLATFORM,
    defaultValue: '3'
  },
  {
    label: '用户账号',
    field: 'userName',
    required: true,
    component: 'Input',
    slot: 'userName',
    dynamicDisabled: ({values}) => {
      return !!values.id;
    },
    // dynamicRules: ({model,schema}) =>rules.duplicateCheckRule("sys_user", "userName",model,schema,true),
  },
  {
    field: 'domain',
    label: '域名',
    helpMessage: '目前保留功能, 尚未实现域名访问',
    component: 'Input',
    slot: 'domain'
  },
  {
    field: 'sceneId',
    label: '场景',
    required: selectTenant.tenantType === TenantType.PLATFORM,
    show: selectTenant.tenantType === TenantType.PLATFORM,
    helpMessage: '租户的应用场景, 比如点餐/CMS',
    component: 'ApiSelect'
  },
  {
    field: 'packageId',
    label: '租户套餐',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择主租户套餐',
    },
    required: true,
    component: 'Select'
  },
  {
    field: 'subPackageId',
    label: '子租户套餐',
    required: true,
    defaultValue: null,
    componentProps: {
      placeholder: '请选择子租户套餐',
    },
    ifShow: ({ values }) => {
      // 平台租户 + 管理场景租户都可以指定子租户套餐, 如果创建的租户类型是管理场景租户则无需指定子租户套餐
      return (selectTenant.tenantType === TenantType.PLATFORM || selectTenant.tenantType === TenantType.MANAGE) && values.tenantType !== TenantType.MANAGE
    },
    component: 'Select'
  },
  {
    field: 'sysBuiltIn',
    label: '系统内置',
    component: 'RadioGroup',
    defaultValue: true,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    }
  },
  {
    field: 'beginTime',
    label: '开始时间',
    helpMessage: '如果不填则不限制',
    component: 'DatePicker',
    ifShow: ({ values }) => !values.isMgtSceneTenant,
    componentProps: {
      // showTime: true,
      valueFormat: 'YYYY-MM-DD',
    }
  },
  {
    field: 'endTime',
    label: '结束时间',
    helpMessage: '如果不填则不限制',
    component: 'DatePicker',
    ifShow: ({ values }) => !values.isMgtSceneTenant,
    componentProps: {
      // showTime: true,
      valueFormat: 'YYYY-MM-DD',
    }
  },
  {
    label: '图标',
    field: 'logo',
    component: 'MyImageUpload',
    componentProps: {
      fileMax:1,
      bizType: UploadBizType.GENERAL_IMAGE
    },
  },
  {
    field: 'status',
    label: '状态',
    defaultValue: '1',
    component: 'JDictSelectTag',
    componentProps: {
      type: "radio",
      dictCode:'data_use_status',
      placeholder:'请选择状态'
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 3,
    },
  }
];





export const formDetailSchema: DescItem[] = [
  {
    field: 'id',
    label: '租户编号',
  },
  {
    field: 'name',
    label: '租户名称',
  },
  {
    field: 'userName',
    label: '用户账号',
  },
  {
    field: 'url',
    label: '域名',
  },
  {
    field: 'packageName',
    label: '租户套餐',
  },
  {
    field: 'sysBuiltIn',
    label: '系统内置',
    render: (curVal, data) => {
      let color = 'green'
      const text = curVal ? '是' : '否'
      return render.renderTag(text, color)
    }
  },
  {
    field: 'beginTime',
    label: '开始时间',
  },
  {
    field: 'endTime',
    label: '结束时间',
  },

  {
    field: 'status',
    label: '状态',
    render: (curVal, data) => {
      const color = curVal === '1' ? 'green' : 'red';
      return render.renderTag(render.renderDict(curVal, 'data_use_status'), color)
    }
  },
  {
    field: 'createdAt',
    label: '创建时间'
  },
  {
    field: 'createdBy',
    label: '创建者'
  },
  {
    field: 'updatedAt',
    label: '更新时间'
  },
  {
    field: 'updatedBy',
    label: '更新者'
  },
  {
    label: '备注',
    labelMinWidth: 40,
    field: 'remark',
  },
  {
    field: 'siteDescTitle',
    label: '描述标题',
  },
  {
    label: '描述内容',
    field: 'siteDescContent',
  }
];